﻿using Demo.UserService.Data;
using Demo.UserService.Entities;
using Demo.UserService.Services.Dtos;
using Mapster;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace Demo.UserService.Repositorys
{

    /// <summary>
    /// 
    /// </summary>
    [Dependency(ServiceLifetime.Singleton)]
    public class UserInfoRepository : EfCoreRepository<MicroserviceDbContext, ShopUserInfo, Guid>, IUserInfoRepository
    {
        
        private readonly IDbContextProvider<MicroserviceDbContext> _dbContextProvider;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbContextProvider"></param>
        public UserInfoRepository(IDbContextProvider<MicroserviceDbContext> dbContextProvider) : base(dbContextProvider)
        {
            _dbContextProvider = dbContextProvider;
        }


        /// <summary>
        /// 注册会员
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ShopUserInfo> AddUserAsync(UserInfoDto dto)
        {

           var  _DbContext = _dbContextProvider.GetDbContextAsync().Result;
            try
            {

                ShopUserInfo userInfo = dto.Adapt<ShopUserInfo>();
                ShopUserAddress address = dto.Adapt<ShopUserAddress>();

                _DbContext.Add(userInfo);
                _DbContext.Add(address);

                var result = await _DbContext.SaveChangesAsync();
                if (result > 0)
                {
                    return userInfo;
                }
                return null;
            }
            catch (Exception e)
            {

                throw;
            }

        }

    }
}
